<?php


namespace app\admin\model;


use think\Db;
use think\Model;

class EmpowerModel extends Model
{
    protected $name = 'empower';

    protected $field = true;

    protected $autoWriteTimestamp = true;   // 开启自动写入时间戳

    /**
     * [getAllCount 根据条件获取所有配置信息数量]
     * @param $map
     * @return int|string
     */

    public function getAllCount($map)

    {

        return $this->alias('r')

            ->join('product p','r.product_version_id = p.id','left')

            ->join('user u','r.u_id = u.u_id','left')

            ->where($map)

            ->count();

    }

    /**
     * [getAllCate 获取全部分类]
     * @param $map
     * @param $Nowpage
     * @param $limits
     * @param $od
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function getAllCate($map, $Nowpage, $limits, $od)

    {

        return $this->alias('r')

            ->field('r.*,p.name as product_name,u.nick_name as u_name')

            ->join('product p','r.product_version_id = p.id','left')

            ->join('user u','r.u_id = u.u_id','left')

            ->where($map)

            ->page($Nowpage, $limits)

            ->order($od)

            ->select();

    }

    /**
     * 添加
     * @param $param
     * @return array
     */

    public function insertObj($param)

    {

        Db::startTrans();// 启动事务

        try {

            $this->allowField(true)->save($param);

            Db::commit();// 提交事务

            writelog('添加成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '添加成功'];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog('添加失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '添加失败'];

        }

    }

    public function getOneObj($id)

    {

        return $this->alias('r')

            ->field('r.*,p.name as product_name,u.nick_name as u_name')

            ->join('product p','r.product_version_id = p.id','left')

            ->join('user u','r.u_id = u.u_id','left')

            ->where('r.id', $id)

            ->find();

    }

    public function editObj($param)

    {

        Db::startTrans();// 启动事务

        try {

            $this->save($param, ['id' => $param['id']]);


            Db::commit();// 提交事务

            writelog('编辑成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '编辑成功'];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog('编辑失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '编辑失败'];

        }

    }

    public function delObj($id)

    {


        Db::startTrans();// 启动事务

        try {

            $this->where('id', $id)->delete();

            Db::commit();// 提交事务

            writelog('删除成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '删除成功'];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog('删除失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '删除失败'];

        }

    }

    //异步修改状态
    public function objState($id, $num)

    {

        $name = Db::name('product')->where(['id' => $id])->value('name');//判断当前状态情况

        if ($num == 2) {

            $msg = '禁用';

        } else {

            $msg = '启用';

        }

        Db::startTrans();// 启动事务

        try {

            $this->where('id', $id)->setField(['status' => $num]);

            Db::commit();// 提交事务

            writelog('【' . $name . '】' . $msg . '成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '已' . $msg];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog('【' . $name . '】' . $msg . '失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => $msg . '失败'];

        }

    }

    //批量删除
    public function batchDel($param)
    {

        Db::startTrans();// 启动事务

        try {

            self::destroy($param);

            Db::commit();// 提交事务

            writelog('批量删除成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog('批量删除失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量删除失败'];

        }

    }

    //批量禁用
    public function forbiddenObj($param)
    {

        Db::startTrans();// 启动事务

        try {

            if ($param) {

                $this->saveAll($param);

            } else {

                $this->where('1=1')->update(['status' => 2]);

            }

            Db::commit();// 提交事务

            writelog('批量禁用分类成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];

        } catch (\Exception $e) {

            Db::rollback();//回滚事务

            writelog('批量禁用分类失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量禁用失败'];

        }

    }

    //批量启用
    public function usingObj($param)
    {

        Db::startTrans();// 启动事务

        try {

            if ($param) {

                $this->saveAll($param);

            } else {

                $this->where('1=1')->update(['status' => 1]);

            }

            Db::commit();// 提交事务

            writelog('批量启用分类成功', 200);

            return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];

        } catch (\Exception $e) {

            Db::rollback();//回滚事务

            writelog('批量启用分类失败', 100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量启用失败'];

        }

    }


}